package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.SmsHomeNewProductApi;
import com.itcat.mapper.SmsHomeNewProductMapper;
import com.itcat.mysql.pojo.SmsHomeBrand;
import com.itcat.mysql.pojo.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeNewProductApiImpl implements SmsHomeNewProductApi {
    @Autowired
    private SmsHomeNewProductMapper smsHomeNewProductMapper;

    @Override
    public Page<SmsHomeNewProduct> list(String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        Page<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(productName != null, SmsHomeNewProduct::getProductName, productName)
                .eq(recommendStatus != null, SmsHomeNewProduct::getRecommendStatus, recommendStatus);
        Page<SmsHomeNewProduct> productPage = smsHomeNewProductMapper.selectPage(page, wrapper);
        return productPage;
    }

    @Override
    public void create(List<SmsHomeNewProduct> smsHomeNewProducts) {
        for (SmsHomeNewProduct smsHomeNewProduct : smsHomeNewProducts) {
            smsHomeNewProductMapper.insert(smsHomeNewProduct);
        }
    }

    @Override
    public void delete(List<Integer> ids) {
        smsHomeNewProductMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateRecommendStatus(List<Integer> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<SmsHomeNewProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SmsHomeNewProduct::getId,ids).set(SmsHomeNewProduct::getRecommendStatus,recommendStatus);
        SmsHomeNewProduct smsHomeNewProduct = new SmsHomeNewProduct();
        smsHomeNewProduct.setRecommendStatus(recommendStatus);
        smsHomeNewProductMapper.update(smsHomeNewProduct,updateWrapper);
    }

    @Override
    public void updateSort(Integer id, Integer sort) {
        SmsHomeNewProduct smsHomeNewProduct = new SmsHomeNewProduct();
        smsHomeNewProduct.setId(id.longValue());
        smsHomeNewProduct.setSort(sort);
        smsHomeNewProductMapper.updateById(smsHomeNewProduct);
    }
}
